home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
bioinformatics.org
/
bioinformatics.org_software.tar
/
www.bioinformatics.org
/
download
/
ecell2
/
ecell220setup.exe
/
{app}
/
standard
/
SRCJ
/
TracerDataManager.java
< prev
Wrap
Text File
|
2002-05-13
|
7KB
|
303 lines
/**
* title: Tracer Data Manager class (TracerDataManager.java)<p>
* description : Save/restor the running data on Tracer<p>
* Copyright (C) 1996-2001 Keio University <p>
* Copyright (C) 1998-2001 Japan Science and Technology Corporation (JST)<p>
* GNU General Public Licence <p>
* Division: Mitui Knowledge Industry Co. Ltd. Bioscience division <p>
* Version : $Id: TracerDataManager.java,v 1.6 2002/05/13 00:23:04 ota Exp $ <p>
*/
package ecell;
import java.util.*;
import java.io.*;
/**
* Class for manage the running data on Tracer
* @author akira shiozawa
* @version 1.0
* @since JDK1.2.2
*/
class TracerDataManager
{
/**
* file extention for tracer data
*/
public static final String szFileExt = ".tdm";
/**
* hashtable for RandomAccessFile object
*/
private /*volatile*/ Hashtable hashType = new Hashtable();
/**
* file write access permission (do NOT access to file if it's true)
*/
private boolean bDel = false;
/**
* directory path for tdm files
*/
public static String szFilePath = "./tdm";
public TracerDataManager()
{
TracerDataManager.createTempDir();
}
/**
* append tracer-data to .tdm(Tracer Data Management) files
* @param type data type
* @param value data to save
* @return boolean
*/
boolean appendTracerData( String id, double[] value ) throws IOException
{
Object fl = null;
boolean ret = false;
if( bDel )
{
return ret;
}
try
{
String fname = id + TracerDataManager.szFileExt;
if( hashType.get( fname ) == null )
{
hashType.put( fname, "" );
File destFile = new File( szFilePath + "/" + fname );
//System.out.println( "destFile:" + fname );
if( destFile.exists() )
{
//System.out.println( "delete destination file:" + fname );
destFile.delete();
}
destFile = null;
}
fl = new RandomAccessFile( szFilePath + "/" + fname, "rw" );
/**
// prepare destination file
fl = hashType.get( fname );
if( fl == null || fl instanceof String )
{
fl = new RandomAccessFile( fname, "rw" );
hashType.put( fname, ((RandomAccessFile)fl) );
}
**/
((RandomAccessFile)fl).seek( ((RandomAccessFile)fl).length() );
// write data into destination file
for( int i = 0; i < value.length; i++ )
{
((RandomAccessFile)fl).writeDouble( value[i] );
}
((RandomAccessFile)fl).close();
ret = true;
}
catch( FileNotFoundException e )
{
e.printStackTrace();
}
catch( IOException e )
{
((RandomAccessFile)fl).close();
throw e;
}
catch( Exception e )
{
e.printStackTrace();
}
return ret;
}
/**
* read tracer-data by block
* @param type data type
* @param value buffer for read data
* @param starindex start posotion for read (0-)
* @param maxsize size of data block to read at a time
* @return int read data size, EOF when -1
*/
int getTracerDataBlock( String id, double[][] value, int startindex, int maxsize )
{
Object fl = null;
int ret = -1;
int size = value[0].length;
byte[] buf = new byte[8 * size * maxsize];
try
{
String fname = id + TracerDataManager.szFileExt;
fl = hashType.get( fname );
if( fl == null || fl instanceof String )
{
fl = new RandomAccessFile( szFilePath + "/" + fname, "rw" );
hashType.put( fname, (RandomAccessFile)fl );
// throw new FileNotFoundException( "file not found :" + fname );
}
//((RandomAccessFile)fl).seek( 8 * size * startindex );
int bufcount = ((RandomAccessFile)fl).read( buf );
//System.out.println( "bufcount:" + bufcount );
//System.out.println( "offset:" + (8 * size * startindex) );
int count = 0;
for( count = 0; count < bufcount / 8; count++ )
{
long data = 0L;
for( int i = 0; i < 8; i++ )
{
data = (data << 8) | (buf[i + count * 8] & 0xFF);
}
//System.out.println( "value[" + (count / size) + "][" + (count % size) + "]" );
value[count / size][count % size] = Double.longBitsToDouble( data );
}
ret = (count == 0)? -1 : count / size;
if( ret < 0 && fl != null )
{
//System.out.println( "---------> " + fname + " closed" );
((RandomAccessFile)fl).close();
hashType.put( fname, "" );
fl = null;
}
//System.out.println( "count:" + count / size );
}
catch( FileNotFoundException e )
{
//e.printStackTrace();
}
catch( IOException e )
{
e.printStackTrace();
}
catch( Exception e )
{
e.printStackTrace();
}
return ret;
}
/**
* delete .tdm files (you must call this method before finishing the program)
* @return void
*/
public void deleteTempFiles()
{
Object fl = null;
String fname = null;
File f = null;
bDel = true;
//System.out.println( "hashType.size():" + hashType.size() );
for( Enumeration enum = hashType.keys(); enum.hasMoreElements(); )
{
fname = (String)enum.nextElement();
//System.out.println( "fname:" + fname );
fl = hashType.get( fname );
try
{
if( fl != null && fl instanceof RandomAccessFile )
{
//System.out.println( "close file:" + fname );
((RandomAccessFile)fl).close();
}
}
catch( Exception e ){ e.printStackTrace(); }
f = new File( TracerDataManager.szFilePath + File.separator + fname );
f.delete();
f = null;
}
}
public static void clearTempDir()
{
ExtensionFilter filter = new ExtensionFilter(
TracerDataManager.szFilePath, TracerDataManager.szFileExt );
String[] list = filter.list();
//System.out.println( "-----------------------------------------------cleartempdir:" + list );
if( list != null )
{
for( int i = 0; i < list.length; i++ )
{
File f = new File( TracerDataManager.szFilePath + File.separator + list[i] );
//System.out.println( "fname:" + list[i] );
if( f.exists() )
{
//System.out.println( "deleted " + list[i] );
f.delete();
}
}
}
}
public static void setTempDir( String path )
{
if( path != null && !path.trim().equals( "" ) )
{
TracerDataManager.szFilePath = path;
}
}
public static void createTempDir()
{
File filedir = new File( TracerDataManager.szFilePath );
if( !filedir.exists() )
{
try
{
filedir.mkdir();
filedir.deleteOnExit();
}
catch( Exception e )
{
e.printStackTrace();
}
}
}
/**
public static void main( String[] args )
{
TracerDataManager cls = new TracerDataManager();
double[][] value = {{1.1,2.2,3.3,4.4},{5.5,6.6,7.7,8.8}};
for( int i = 0; i < value.length; i++ )
{
cls.appendTracerData( 1, value[i] );
}
double result[][] = new double[100][4];
int count = cls.getTracerDataBlock( 1, result, 0, 100 );
System.out.println( "--------> count : " + count );
for( int i = 0; i < count; i++ )
{
for( int j = 0; j < result[i].length; j++ )
{
System.out.println( "value:" + result[i][j] );
}
System.out.println( "-----------------------" );
}
count = cls.getTracerDataBlock( 1, result, 100, 100 );
System.out.println( "end:" + count );
//cls.deleteTempFiles();
}
**/
}